import React, { Component } from 'react'
import "./index.css"

export default class Footer extends Component {
/*   state = {
    checkAll: false
  } */

  // 全选的回调
  /* handleCheckAll = () => {
    const {checkAll} = this.state
    this.setState({
      checkAll: !checkAll
    })
    this.props.checkAllTodo(checkAll)
  } */

  handleCheckAll = (event) => {
    this.props.checkAllTodo(event.target.checked)
  }

  handleClearAllDone = () => {
    this.props.clearAllDone()
  }

  render() {
    const {todos} = this.props;
    // const doneCount = todos.filter(todo => todo.done === true).length
    const doneCount = todos.reduce((prev, curr) => { return prev + (curr.done ? 1 : 0) }, 0);
    const total = todos.length;

    return (
      <div className='todo-footer'>
        <label>
          {/* defaultChecked 只执行第一次 */}
          {/* <input type='checkbox' defaultChecked={ doneCount === total ? true : false} onChange="{handleChange}"/> */} 
          <input type='checkbox' checked={ doneCount === total && total !== 0 ? true : false} onChange={this.handleCheckAll}/>
        </label>
        <span>
          <span>已完成{doneCount}</span> / 全部{total}
        </span>
        <button className='btn btn-danger' onClick={this.handleClearAllDone}>清除已完成任务</button>
      </div>
    )
  }
}
